<?php

declare(strict_types=1);

namespace app\admin\model;

use think\Model;

class TransactionCurrency extends Model
{
    // 定义数据表名
    protected $name = 'transaction_currency';

    // 定义主键
    protected $primaryKey = 'id';

    // 定义创建时间字段名
    protected $createTime = 'create_time';

    // 定义更新时间字段名
    protected $updateTime = 'update_time';
    // 定义自动时间戳
    protected $autoWriteTimestamp = true;

    // 设置字段信息
    protected $schema = [
        'id'          => 'int',
        'name'        => 'string',
        'abbr'        => 'string',
        'status'      => 'int',
        'is_del'      => 'int',
        'create_time' => 'datetime',
        'creator'     => 'int',
        'update_time' => 'datetime',
        'updater'     => 'int',

    ];



    // 定义一个通用的访问器来获取属性值
    protected $attributes = [
        'status' => [
            0 => '关闭',
            1 => '开启'
        ],
    ];

    public function getAttr($name)
    {
        // 检查是否是要处理的特殊属性
        if (strpos($name, '_value') !== false) {
            $originalField = str_replace('_value', '', $name);
            if (isset($this->attributes[$originalField])) {
                $value = $this->getData($originalField);
                return $this->attributes[$originalField][$value] ?? '未知';
            }
        }

        // 如果不是特殊属性，调用父类的方法
        return parent::getAttr($name);
    }


    public function getFieldEnum($field)
    {

        if ('ALL' === strtoupper($field)) {
            return [
                'status' => $this->attributes['status'],
            ];
        } else {
            return [$field => $this->$field];
        }
    }


    // 定义关联方法
    public function creator()
    {
        // 假设'creator'是外键字段，'id'是Customer模型的主键
        return $this->belongsTo('Customer', 'creator', 'id');
    }

    public function updater()
    {
        // 假设'updater'是外键字段，'id'是Customer模型的主键
        return $this->belongsTo('Customer', 'updater', 'id');
    }

    // 定义获取器
    public function getCreatorValueAttr($value, $data)
    {
        return $this->getRelatedCustomerName('creator');
    }

    public function getUpdaterValueAttr($value, $data)
    {
        return $this->getRelatedCustomerName('updater');
    }

    // 私有辅助方法
    private function getRelatedCustomerName($relationName)
    {
        $relation = $this->getRelation($relationName);

        if (!$relation) {
            $relation = $this->$relationName()->find();
        }

        return $relation ? $relation->cusname : null;
    }

    /**
     * @describe 获取下拉框格式数据
     * @author: Bobo
     * @date: 2024-08-23 10:06:27
     * @param [type] @required
     * @return Renderable
     */
    public function getSelectOption(array $where, $fields = 'id, name')
    {
        $data = $this
            ->field($fields)
            ->where($where)
            ->select()
            ->toArray();

        $selectOption = [];
        foreach ($data as $option) {
            $selectOption[] = [
                'label' => $option['name'],
                'value' => intval($option['id'])
            ];
            // $selectOption[intval($option['id'])] = $option['name'];
        }
        return $selectOption;
    }
}
